﻿using Deployment.Data.UI.Dialogs.Variable;
using Deployment.Data.Variable;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Deployment.Data.UI.Controls.Variable
{
    /// <summary>
    /// Interaction logic for EditableVariableList.xaml
    /// </summary>
    public partial class EditableVariableList : UserControl, INotifyPropertyChanged
    {
        public static readonly DependencyProperty SelectedVariableProperty =
            DependencyProperty.Register("SelectedVariable",
                typeof(AbstractVariable),
                typeof(EditableVariableList));

        public static readonly DependencyProperty VariablesProperty =
            DependencyProperty.Register("Variables",
                typeof(VariablesFile),
                typeof(EditableVariableList));


        public static readonly DependencyProperty EnvironmentsProperty =
            DependencyProperty.Register("Environments",
                typeof(Data.Environment.EnvironmentsFile),
                typeof(EditableVariableList));

        public event PropertyChangedEventHandler PropertyChanged;
        private void SetValueDp(DependencyProperty property, object value)
        {
            SetValue(property, value);
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(property.Name));
        }


        public EditableVariableList()
        {
            InitializeComponent();
            DataContext = this;
        }

        public AbstractVariable SelectedVariable
        {
            get { return (AbstractVariable)GetValue(SelectedVariableProperty); }
            set { SetValueDp(SelectedVariableProperty, value); }
        }

        public Data.Environment.EnvironmentsFile Environments
        {
            get { return (Data.Environment.EnvironmentsFile)GetValue(EnvironmentsProperty); }
            set { SetValueDp(EnvironmentsProperty, value); }
        }

        public VariablesFile Variables
        {
            get { return (VariablesFile)GetValue(VariablesProperty); }
            set 
            {
                if (value == null || !(value is VariablesFile))
                    value = new VariablesFile();
                SetValueDp(VariablesProperty, value); 
            }
        }

        private void VariableList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var item = VariableList.SelectedItem as AbstractVariable;
            SelectedVariable = item;
        }

        private void RemoveSelected_Click(object sender, RoutedEventArgs e)
        {
            VariablesEditorControl parent = WPFHelpers.TryFindParent<VariablesEditorControl>(this);
            
            if (Variables != null)
            {
                AbstractVariable variable = SelectedVariable;
                if (variable != null)
                {
                    MessageBoxResult result = MessageBox.Show(Window.GetWindow(this),
                        string.Format("Are you sure you want to remove the selected variable?"),
                        "Confirm Delete", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        Variables.RemoveVariable(variable);
                        if (parent != null)
                            parent.HasUnsavedChanges = true;
                    }
                }
            }
        }

        private void AddVariable_Click(object sender, RoutedEventArgs e)
        {
            VariableEditorDialog dlg = new VariableEditorDialog(this.Variables, this.Environments);
            VariablesEditorControl parent = WPFHelpers.TryFindParent<VariablesEditorControl>(this);
            dlg.Owner = Window.GetWindow(this);
            
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result == true)
            {
                this.Variables.AddVariable(dlg.newVariable);
                if (parent != null)
                    parent.HasUnsavedChanges = true;
            }
        }
    }
}
